<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\RoomTagPull\Action\Dashboard\Traits;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\App\WorkContact\Contract\WorkContactEmployeeContract;
use MoChat\App\WorkContact\Contract\WorkContactRoomContract;
use MoChat\App\WorkContact\Contract\WorkContactTagPivotContract;
use Psr\Container\ContainerInterface;

trait UpdateTrait
{
    /**
     * @Inject
     * @var WorkContactContract
     */
    protected $workContactService;

    /**
     * @Inject
     * @var WorkContactRoomContract
     */
    protected $workContactRoomService;

    /**
     * @Inject
     * @var WorkContactEmployeeContract
     */
    protected $workContactEmployeeService;

    /**
     * @Inject
     * @var WorkContactTagPivotContract
     */
    protected $workContactTagPivotService;

    /**
     * @var RequestInterface
     */
    protected $request;

    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(RequestInterface $request, ContainerInterface $container)
    {
        $this->request = $request;
        $this->container = $container;
    }

    /**
     * 过滤客户.
     */
    protected function filterContact(int $filterContact, array $rooms, array $employees, array $chooseContact): array
    {
        // 所有客户
        $contactIds = $this->chooseContact($employees, $chooseContact);
        if (empty($contactIds)) {
            return [];
        }
        // 不过滤
        if ($filterContact === 0) {
            return $contactIds;
        }
        // 过滤已在群聊中的客户
        if ($filterContact === 1) {
            $roomIds = array_column($rooms, 'id');
            $contactRoom = $this->workContactRoomService->getWorkContactRoomsByRoomIdsContactIdsStatus($roomIds, $contactIds, 1, ['contact_id']);
            if (! empty($contactRoom)) {
                $delContactIds = array_unique(array_column($contactRoom, 'contactId'));
                $contactIds = array_diff($contactIds, $delContactIds);
            }
        }
        return $contactIds;
    }

    private function chooseContact(array $employees, array $params): array
    {
        // 全部客户
        if ($params['isAll'] === 0) {
            $contactEmployee = $this->workContactEmployeeService->getWorkContactEmployeesByEmployeeIdsStatus($employees, '', '', 1, ['contact_id']);
            if (empty($contactEmployee)) {
                return [];
            }
            return array_unique(array_column($contactEmployee, 'contactId'));
        }
        // 员工客户&时间筛选
        $contactEmployee = $this->workContactEmployeeService->getWorkContactEmployeesByEmployeeIdsStatus($employees, $params['startTime'], $params['endTime'], 1, ['contact_id']);
        if (empty($contactEmployee)) {
            return [];
        }
        // 客户id
        $contactIds = array_unique(array_column($contactEmployee, 'contactId'));

        // 标签
        if (! empty($params['tagIds'])) {
            $contacts = $this->workContactTagPivotService->getWorkContactTagPivotsByContactIdsEmployeeIdsTagIds($contactIds, $employees, $params['tagIds'], 1, ['contact_id']);
            if (empty($contacts)) {
                return [];
            }
            $contactIds = array_unique(array_column($contacts, 'contactId'));
        }
        // 性别
        if ($params['gender'] !== '') {
            $contacts = $this->workContactService->getWorkContactsByIdsGender($contactIds, (int) $params['gender'], ['id']);
            if (empty($contacts)) {
                return [];
            }
            $contactIds = array_unique(array_column($contacts, 'id'));
        }
        return $contactIds;
    }
}
